Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Passing environment as an argument to eng_r #2358

Merged
merged 9 commits into from
Nov 1, 2024
Merged

Conversation

abhsarma
Copy link
Contributor

I am trying to add a feature to my R package where each code chunk implements multiple analysis (and each analysis is executed in a separate environment). This presents some complications during knitting as every analysis is executed in the global_env():

knitr/R/block.R

Line 189 in fb1f423

env = knit_global()

I am wondering if it is possible to allow env be passed as an optional argument to eng_r() which defaults to global_env(). I've made a PR (#) with the relevant changes, but please let me know if this creates other issues or if there is a better way of doing this.

Thanks!

Copy link
Owner

@yihui yihui left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

knit_global() is used in many places in this package. I guess this change is not enough.

I can provide an API to change the global environment (e.g., knit_global(envir) to change to a custom envir). Would that meet your need?

@abhsarma
Copy link
Contributor Author

abhsarma commented Aug 1, 2024

I think so? This change was sufficient for what I was trying to achieve, but I am obviously not as familiar with the knitr internals as you are and there may be scenarios which I have not thought of, so I defer to what you think is best :)

Copy link
Owner

@yihui yihui left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now you can change the environment via knit_global(envir). I guess the good practice is to record the old envir, set the new envir, and restore the envir later, e.g.,

old_env = knit_global(new_env)
on.exit(knit_global(old_env), add = TRUE)
# do your stuff in new_env

@yihui yihui merged commit c81043b into yihui:master Nov 1, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants